package com.ctg.edas.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/nacos/provider")
public class ProviderController {

    private static final Logger logger = LoggerFactory.getLogger(ProviderController.class);

    @Autowired
    private AppOneFeign appOneFeign;

    @GetMapping("/echo")
    public String echo() {
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        }  catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(ip)) {
            logger.info("Spring Cloud服务端");
            return "Spring Cloud服务端";
        }
        logger.info("Spring Cloud服务端 [{}]", ip);
        return "Spring Cloud服务端 [" + ip + "]";
    }

    @GetMapping("/alive")
    public Boolean alive() {
        logger.info("/alive");
        return Boolean.TRUE;
    }

    @GetMapping("/slow")
    public void slow(@RequestParam(value = "second", defaultValue = "5") Integer second) {
        logger.info("/slow");
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/istio")
    public String istio() {
        logger.info("/istio");
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        }  catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(ip)) {
            logger.info("ISTIO 服务端");
            return "ISTIO 服务端";
        }
        logger.info("ISTIO 服务端 [{}]", ip);
        return "ISTIO 服务端 [" + ip + "]";
    }


    @GetMapping("/echogo")
    public String echogo(@RequestParam(defaultValue = "1") String userId) {
        logger.info("/echogo");

        String rs = "";

        try {
            rs = appOneFeign.hp1(userId);
            Class c = this.getClass().getClassLoader().loadClass("com.ctg.mse.istio.xds.XdsClient");
            Method getMethod = c.getDeclaredMethod("getClient", null);
            Object client = getMethod.invoke(null, null);

            Field field = c.getDeclaredField("serviceEndpoints");
            field.setAccessible(true);
            ConcurrentHashMap result = (ConcurrentHashMap) field.get(client);
            result.put("123123", "123123");
            System.out.println(result.toString());

        }catch (Exception e){
            System.out.println("get class error");
        }

        logger.info("echogo 返回 [{}]", rs);
        return "echogo 返回 [" + rs + "]";
    }
}
